{extend name="base"/}
{block name="resources"}
<style>
    #container{height: 500px; }
    .address-content {display: inline-block;vertical-align: top;}
</style>
{/block}
{block name="main"}

<div class="layui-form ns-form" lay-filter="storeform">
    <input type="hidden" name="address_id" autocomplete="off" value="{$info.id}">
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>联系人：</label>
        <div class="layui-input-block">
            <input type="text" name="contact_name" autocomplete="off" lay-verify="required" class="layui-input ns-len-long" value="{$info.contact_name}">
        </div>
        <div class="ns-word-aux">联系人</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>联系人手机号：</label>
        <div class="layui-input-block">
            <input type="text" name="mobile" autocomplete="off" lay-verify="phone" class="layui-input ns-len-long" value="{$info.mobile}">
        </div>
        <div class="ns-word-aux">联系人手机号</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required"></span>邮政编码：</label>
        <div class="layui-input-block">
            <input type="text" name="postcode" autocomplete="off" lay-verify="postcode" class="layui-input ns-len-long" value="{$info.postcode}">
        </div>
        <div class="ns-word-aux">邮政编码</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>联系地址：</label>
        <div class="layui-input-inline ns-len-mid area-select">
            <select name="province_id" lay-filter="province_id" lay-verify="required">
                <option value="">请选择省份</option>
                {foreach $province_list as $k => $v}
                <option value="{$v.id}">{$v.name}</option>
                {/foreach}
            </select>
        </div>
        <div class="layui-input-inline ns-len-mid area-select">
            <select name="city_id" lay-filter="city_id" lay-verify="required">
                <option value="">请选择城市</option>
            </select>
        </div>

        <div class="layui-input-inline ns-len-mid area-select">
            <select name="district_id" lay-filter="district_id" lay-verify="required">
                <option value="">请选择区/县</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label"><span class="required">*</span>详细地址：</label>
        <div class="layui-input-block">
            <input type="text" name="address" autocomplete="off" lay-verify="required" class="layui-input ns-len-long" value="{$info.address}">
        </div>
        <div class="ns-word-aux">详细地址</div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">地址类型：</label>
        <div class="layui-input-block" id="work_week">
            {if $info['is_return'] == 0}
                <input type="checkbox" name="is_return" value="1" title="退货地址" lay-skin="primary">
            {else /}
                <input type="checkbox" name="is_return" value="1" title="退货地址" lay-skin="primary" checked>
            {/if}
            {if $info['is_delivery'] == 0}
                <input type="checkbox" name="is_delivery" value="2" title="发货地址" lay-skin="primary">
            {else /}
                <input type="checkbox" name="is_delivery" value="2" title="发货地址" lay-skin="primary" checked>
            {/if}
        </div>
    </div>
    <div class="ns-form-row">
        <button class="layui-btn ns-bg-color" lay-submit lay-filter="save">保存</button>
    </div>

</div>
{/block}

{block name="script"}
<script type="text/javascript" src="SHOP_JS/address.js"></script>
<script type="text/javascript" src="STATIC_JS/map_address.js"></script>
<script type="text/javascript">
    var form, laydate, repeat_flag;
    layui.use(['laydate', 'form'], function() {
        form = layui.form;
        laydate = layui.laydate;
        repeat_flag = false;//防重复标识

        form.render();

        //初始化联系地址
        var initdata = {province_id : '{$info.province_id}', city_id : '{$info.city_id}', district_id : '{$info.district_id}'};
        initAddress(initdata, "storeform");
        form.on('submit(save)', function(data) {

            var province_name = $("select[name=province_id] option:selected").text();
            var city_name = $("select[name=city_id] option:selected").text();
            var district_name = $("select[name=district_id] option:selected").text();

            //地址id
            data.field.province = data.field.province_id;
            data.field.city = data.field.city_id;
            data.field.district = data.field.district_id;
            data.field.community = data.field.community_id;

            //地址name
            data.field.province_name = province_name;
            data.field.city_name = city_name;
            data.field.district_name = district_name;

            data.field.full_address = province_name + city_name + district_name;
            $.ajax({
                type : "POST",
                dataType: 'JSON',
                url : ns.url("shop/shopaddress/editAddress"),
                async : true,
                data : data.field,
                success : function(res) {
                    repeat_flag = false;

                    if (res.code == 0) {
                        layer.confirm('编辑成功', {
                            title: '操作提示',
                            btn: ['返回列表', '继续编辑'],
                            yes: function() {
                                location.href = ns.url("shop/shopaddress/lists")
                            },
                            btn2: function() {
                                location.href = ns.url("shop/shopaddress/editAddress",{"address_id":data.field.address_id});
                            }
                        });
                    } else {
                        layer.msg(res.message);
                    }
                }
            })
        });

        //表单验证
        form.verify({
            province_id : function(value, item){
                if(value == ''){
                    return '请选择省份';
                }
            },
            city_id : function(value, item){
                if(value == ''){
                    return '请选择城市';
                }
            },
            district_id : function(value, item){
                if(value == ''){
                    return '请选择区/县';
                }
            },
            community_id : function(value, item){
                if(value == ''){
                    return '请选择街道';
                }
            },
            address: function(value) {
                if (value == '') {
                    return '请输入详细地址';
                }
            },
        });
    });

</script>
{/block}